package com.springmq.mq.thread;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import sun.nio.ch.ThreadPool;

/**
 * @ClassName : ThreadTicket
 * @Author : lby
 * @Date: 2021/1/29 14:52
 * @Description : 多线程-火车票测试
 *  发现问题：当多个线程同时操作一个对象是，出现数据紊乱的问题，需要线程同步解决
 */
@Data
@Slf4j
public class ThreadTicket implements Runnable {

    /**
     * 火车票的数量
     */
    private int ticketCounts=10;

    @SneakyThrows
    @Override
    public void run() {
        // 买票
        for (int i = 1; i <ticketCounts; i++) {
            // 查看当前线程
            Thread.sleep(500);
            log.info("当前是:{},买到了第:{}张票",Thread.currentThread().getName(),i);
        }
    }

    /**
     * 测试买票逻辑
     */
    public static void main(String[] args) {
        ThreadTicket threadTicket = new ThreadTicket();
        new Thread(threadTicket,"张三").start();
        new Thread(threadTicket,"李四").start();
        new Thread(threadTicket,"王五").start();

    }
}
